import { contextBridge } from 'electron'
import { electronAPI } from '@electron-toolkit/preload'

const { createAgoraRtcEngine } = require('agora-electron-sdk')

// Custom APIs for renderer
const api = { date: new Date(), number: Math.random() }

const rtcEngine = createAgoraRtcEngine()
process.stdout.write('preload calling\n')

// Use `contextBridge` APIs to expose Electron APIs to
// renderer only if context isolation is enabled, otherwise
// just add to the DOM global.
if (process.contextIsolated) {
  try {
    contextBridge.exposeInMainWorld('electron', electronAPI)
    contextBridge.exposeInMainWorld('api', api)
    contextBridge.exposeInMainWorld('rtcEngine', {
      initialize: () => rtcEngine.initialize
    })
  } catch (error) {
    console.error(error)
  }
} else {
  // @ts-ignore (define in dts)
  window.electron = electronAPI
  // @ts-ignore (define in dts)
  window.api = api
  // @ts-ignore (define in dts)
  window.rtcEngine = rtcEngine
}
